package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.common.Response;
import org.example.controller.req.LoginReq;
import org.example.controller.req.RegisterReq;
import org.example.controller.resp.LoginResp;
import org.example.entity.EmailCode;
import org.example.entity.StUser;
import org.example.enums.RedisUrlEnum;
import org.example.mapper.EmailCodeMapper;
import org.example.mapper.StUserMapper;
import org.example.service.EmailCodeService;
import org.example.service.StUserService;
import org.example.util.JWTUtil;
import org.example.util.RedisUtil;
import org.example.util.SendEmailCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Random;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    private SendEmailCodeUtil sendEmailCodeUtil;
    @Resource
    private EmailCodeService emailCodeService;
    @Resource
    private EmailCodeMapper emailCodeMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private StUserService stUserService;
    @Resource
    private StUserMapper stUserMapper;
    @Resource
    private JWTUtil jwtUtil;

    @PostMapping("/sendCode")
    public Response<Boolean> sendCode(@RequestParam("email")String email){
        Random random = new Random();
        int code = 1000 + random.nextInt(9000);
        sendEmailCodeUtil.sendCodeEmail(email,String.valueOf(code));
//        EmailCode emailCode = new EmailCode();
//        emailCode.setEmail(email);
//        emailCode.setCaptcha(String.valueOf(code));
//        emailCodeMapper.insert(emailCode);
        redisUtil.setValueWithExpire(RedisUrlEnum.SEND_EMAIL.getKey() + email, String.valueOf(code),300L);
        return Response.success(true);
    }

    @PostMapping("/register")
    public Response<Boolean> register(@RequestBody @Valid RegisterReq req){
        if(!req.getPwd().equals(req.getConfirmPwd())){
            throw new RuntimeException("两次输入的密码不一致");
        }
        Boolean exist = redisUtil.ifExistKeyAndValue(RedisUrlEnum.SEND_EMAIL.getKey() + req.getEmail(), req.getCode());
        if(exist){
            StUser stUser = new StUser();
            stUser.setEmail(req.getEmail());
            QueryWrapper<StUser> wrapper = new QueryWrapper<>();
            wrapper.setEntity(stUser);
            StUser result = stUserMapper.selectOne(wrapper);
            if(result != null ){
                throw new RuntimeException("该邮箱已被注册");
            }
            stUser.setPassword(req.getConfirmPwd());
            stUser.setLoginName(req.getLoginName());
            stUserService.save(stUser);
            return Response.success(true);
        }
        throw new RuntimeException("error code");
    }

    @PostMapping("/userLogin")
    public Response<LoginResp> login(@RequestBody @Valid LoginReq req){
        QueryWrapper<StUser> wrapper = new QueryWrapper<>();
        StUser stUser = new StUser();
        stUser.setEmail(req.getEmail());
        wrapper.setEntity(stUser);
        StUser user = stUserMapper.selectOne(wrapper);
        if(user != null && user.getPassword().equals(req.getPwd())){
            String token = jwtUtil.sign(user.getId());
            LoginResp loginResp = new LoginResp();
            loginResp.setToken(token);
            loginResp.setUserId(user.getId());
            loginResp.setUserName(user.getLoginName());
            redisUtil.setValueWithExpire(RedisUrlEnum.LOGIN_TOKEN.getKey() + req.getEmail(), token,60*60*24*30L);
            return Response.success(loginResp);
        }
        throw new RuntimeException("error pwd or email");
    }
}
